package com.briup.pts.web.controller;


import com.briup.pts.aop.Logger;
import com.briup.pts.config.response.Result;
import com.briup.pts.domain.bean.FenceHouse;
import com.briup.pts.domain.vo.FenceHouseVO;
import com.briup.pts.service.FenceHouseService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "栏舍管理模块")
@RestController
@RequestMapping("/fenceHouse")
@RequiredArgsConstructor
public class FenceHouseController {
    private final FenceHouseService fenceHouseService;


    @Operation(summary = "分页多条件查询栏舍信息接口")
    @GetMapping("/query")
    public Result<PageInfo<FenceHouse>> pageQuery(
            @RequestParam(defaultValue = "1")
            @Min(value = 1, message = "当前页码不能小于1") Integer pageNum,
            @RequestParam(defaultValue = "5")
            @Min(value = 1, message = "页面不能小于1") Integer pageSize,
            String fhName){

        PageInfo<FenceHouse> pageInfo = fenceHouseService
                .pageQueryByConditional(pageNum, pageSize, fhName);

        return Result.success(pageInfo);
    }

    @Operation(summary = "根据栏舍编号查询栏舍信息以及栏圈信息接口")
    @GetMapping("/selectById")
    public Result<?> selectByfhId(String fhId) {
        FenceHouseVO fenceHouseVO = fenceHouseService.selectByfhId(fhId);
        return Result.success(fenceHouseVO);
    }

    @Logger("新增或修改栏舍接口")
    @Operation(summary = "新增或修改栏舍接口")
    @PostMapping("/saveOrUpdate")
    public Result<?> saveOrUpdate(@RequestBody @Valid FenceHouse fenceHouse){
        fenceHouseService.saveOrUpdate(fenceHouse);
        return Result.success();
    }

    @Logger("根据id删除栏舍信息接口")
    @Operation(summary = "根据id删除栏舍信息接口")
    @DeleteMapping("/deleteById/{fhId}")
    public Result<?> deleteById(@PathVariable
                                @NotBlank(message = "栏舍id不能为空") String fhId){
        fenceHouseService.batchDelete(List.of(fhId));
        return Result.success();
    }

    @Logger("批量删除栏舍接口")
    @Operation(summary = "批量删除栏舍接口")
    @DeleteMapping("/deleteByIdAll")
    public Result<?> deleteByIdAll(@RequestBody
                                   @NotEmpty(message = "栏舍id必须携带") List<String> ids){
        fenceHouseService.batchDelete(ids);
        return Result.success();
    }
}
